﻿using MediatR;
using StoreIModels;
using StoreISet.Applicationes.Command;
using StoreRepositorys.Interface;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace StoreISet.Applicationes.CommandHandler
{
    public class SystematicsortUpdCommandler : IRequestHandler<SystematicsortUpdCommand, int>
    {
        private readonly ISystematicRepository _systematicRepository;

        public SystematicsortUpdCommandler(ISystematicRepository systematicRepository)
        {
            _systematicRepository = systematicRepository;
        }

        public async Task<int> Handle(SystematicsortUpdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var systematic = await _systematicRepository.GetModelasync(request.SystematicId);
                if(systematic == null)
                {
                    systematic = new SystematicModel() { SystematicId = request.SystematicId };

                }

                systematic.Systematicsort = request.Systematicsort;

                return await _systematicRepository.Updateasync(systematic);

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
